﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

using System.Data;
using Microsoft.Reporting.WebForms;

public partial class Stdby_Reportes_costoTransporteBien : System.Web.UI.Page
{
    private void mostrarMensaje(string mensaje)
    {
        System.Text.StringBuilder sbMensaje = new System.Text.StringBuilder();

        sbMensaje.Append("<script type='text/javascript'>");
        sbMensaje.AppendFormat("alert('{0}');", mensaje);
        sbMensaje.Append("</script>");
        ClientScript.RegisterClientScriptBlock(this.GetType(), "Mensaje", sbMensaje.ToString());
    }



    /*---------------------------------------------------------*/


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cargarComboIdTipoElemento();
            cargarComboAnio();
        }
    }



    private void cargarComboIdTipoElemento()
    {
        try
        {
            string consulta = "SELECT '' AS 'Id', '' As NombElem UNION SELECT Id, NombElem FROM tipoelemento";

            SqlDataAdapter daMovex = new SqlDataAdapter(consulta, CCadenaConexionSql.cadConexion());

            DataSet dsMovex = new DataSet();
          
            daMovex.Fill(dsMovex);

            ddlIdTipoElemento.DataSource = dsMovex.Tables[0].DefaultView;
            ddlIdTipoElemento.DataTextField = "NombElem";
            ddlIdTipoElemento.DataValueField = "Id";
            ddlIdTipoElemento.DataBind();
        }
        catch { }
    }

    private void cargarComboAnio()
    {
        try
        {
            ddlAnio.Items.Add("");

            int i = 1980;

            while (i <= 2060){
                ddlAnio.Items.Add(i.ToString());
                i++;
            }

            
        }
        catch { }
    }

    /*mostrar reporte*/

    protected void btnMostrarReporte_Click(object sender, EventArgs e)
    {
        ReportViewer1.Visible = true;
        reporte();
    }

    private void reporte()
    {
        try
        {

            ReportDataSource rds = new ReportDataSource();

            rds.Name = "dsCostoTransporteBien";//datset del .rdlc
            rds.Value = darDataSet().Tables[0];

            // Donde fvFact es el control de ReportViewer de la página.
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("costoTransporteBien.rdlc");

            ReportViewer1.LocalReport.Refresh();


        }
        catch (Exception e)
        {
            string pp = e.Message.ToString();
        }


    }



    private DataSet darDataSet()
    {
        DataSet myDS = new DataSet();

        using (SqlConnection objConexion = new SqlConnection(CCadenaConexionSql.cadConexion()))
        {
            try
            {

                SqlCommand MyCommand = new SqlCommand();
                MyCommand.Connection = objConexion;
                MyCommand.CommandType = CommandType.StoredProcedure;
                MyCommand.CommandText = "Reporte_CostoDeTransportePorBien";

                MyCommand.Parameters.Clear();


                if (!String.IsNullOrEmpty(ddlIdTipoElemento.SelectedItem.Text.Trim()) )
                {
                    MyCommand.Parameters.AddWithValue("@Bien", ddlIdTipoElemento.SelectedValue.Trim());
                }
                else
                {
                    MyCommand.Parameters.AddWithValue("@Bien", null);
                }

                if (!String.IsNullOrEmpty(ddlMes.SelectedItem.Text.Trim()))
                {
                    MyCommand.Parameters.AddWithValue("@Mes", ddlMes.SelectedIndex);
                }
                else
                {
                    MyCommand.Parameters.AddWithValue("@Mes", null);

                }

                if (!String.IsNullOrEmpty(ddlAnio.SelectedItem.Text.Trim()))
                {
                    MyCommand.Parameters.AddWithValue("@Anio", ddlAnio.SelectedItem.Text.Trim());
                }
                else
                {
                    MyCommand.Parameters.AddWithValue("@Anio", null);

                }

                

                SqlDataAdapter MyDA = new SqlDataAdapter();
                MyDA.SelectCommand = MyCommand;

                if (objConexion.State == ConnectionState.Closed)
                { objConexion.Open(); }


                //nombre del dataset  a rellenar
                MyDA.Fill(myDS, "dsCostoTransporteBien");


            }
            catch { }

            if (objConexion.State == ConnectionState.Open)
            { objConexion.Close(); }

            objConexion.Dispose();
        }

        return myDS;
    }

}